<!DOCTYPE html>



  


<html class="theme-next pisces use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">



  
  
    
    
  <script src="/lib/pace/pace.min.js?v=1.0.2"></script>
  <link href="/lib/pace/pace-theme-minimal.min.css?v=1.0.2" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="node.js,">










<meta name="description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。">
<meta name="keywords" content="node.js">
<meta property="og:type" content="article">
<meta property="og:title" content="node.js学习笔记">
<meta property="og:url" content="https://shellingfordly.gitee.io/2019/09/29/node-js学习笔记/index.html">
<meta property="og:site_name" content="shelllingfordly">
<meta property="og:description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-10-10T04:13:20.787Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="node.js学习笔记">
<meta name="twitter:description" content="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://shellingfordly.gitee.io/2019/09/29/node-js学习笔记/">





  <title>node.js学习笔记 | shelllingfordly</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">shelllingfordly</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">人生本就是一场孤独的旅行</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-首页">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-归档">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-分类">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-标签">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-评论区">
          <a href="/comment" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-comment"></i> <br>
            
            评论区
          </a>
        </li>
      
        
        <li class="menu-item menu-item-关于我">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于我
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://shellingfordly.gitee.io/2019/09/29/node-js学习笔记/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="shellingford ly">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/uploads/images/header.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="shelllingfordly">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">node.js学习笔记</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-29T01:07:10+08:00">
                2019-09-29
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/" itemprop="url" rel="index">
                    <span itemprop="name">学习</span>
                  </a>
                </span>

                
                
                  ， 
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/学习/node-js/" itemprop="url" rel="index">
                    <span itemprop="name">node.js</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/09/29/node-js学习笔记/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/09/29/node-js学习笔记/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <span>
&nbsp;&nbsp;&nbsp;&nbsp;
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。
Node.js 使用了一个事件驱动、非阻塞式 I/O 的模型，使其轻量又高效。
</span>

<a id="more"></a>


<h2 id="了解node-js"><a href="#了解node-js" class="headerlink" title="了解node.js"></a>了解node.js</h2><blockquote>
<p>node.js使用了一个事件驱动、非阻塞式I/O的模型，使其轻量又高效。 </p>
</blockquote>
<ul>
<li>I/O密集</li>
<li>CPU密集</li>
</ul>
<blockquote>
<p>BS 浏览器和后端<br>CS 客户端和后端</p>
</blockquote>
<h3 id="关于node"><a href="#关于node" class="headerlink" title="关于node"></a>关于node</h3><ul>
<li>node没有window、document</li>
<li>global是顶层对象</li>
</ul>
<h3 id="安装nodejs"><a href="#安装nodejs" class="headerlink" title="安装nodejs"></a>安装nodejs</h3><ul>
<li>安装</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> 复制回车就行</span><br><span class="line">wget https://nodejs.org/dist/v10.8.0/node-v10.8.0-linux-x64.tar.xz</span><br><span class="line"><span class="meta">#</span> 回车，等待安装成功</span><br></pre></td></tr></table></figure>

<ul>
<li>解压</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">tar xvf node-v10.7.0-linux-x64.tar.xz</span><br></pre></td></tr></table></figure>

<ul>
<li>移动</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#</span> mv命令移动并改名</span><br><span class="line">mv node-v10.7.0-linux-x64 /usr/local/node</span><br></pre></td></tr></table></figure>

<ul>
<li>配置命令</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">vi ~/.bash_profile</span><br><span class="line"><span class="meta">#</span> 在 ~/.bash_profile 回车打开的文件里按 i 编辑，然后在文件的export PATH前一行添加</span><br><span class="line">PATH=$PATH:/usr/local/node/bin</span><br><span class="line"><span class="meta">#</span> 然后退出保存文件 :wq</span><br><span class="line"><span class="meta">#</span> 运行</span><br><span class="line">source ~/.bash_profile</span><br><span class="line"></span><br><span class="line"><span class="meta">#</span> node -v npm -v 查看node和npm命令是否配置成功</span><br></pre></td></tr></table></figure>

<ul>
<li>到此nodejs安装成功</li>
</ul>
<h3 id="引用require"><a href="#引用require" class="headerlink" title="引用require"></a>引用require</h3><ul>
<li>缓存：在同一个模块中，第一次引入一个模块时，会有缓存；第二次引用时不会再去调用模块，而是直接使用第一次调用过的模块</li>
</ul>
<h3 id="输出exports"><a href="#输出exports" class="headerlink" title="输出exports"></a>输出exports</h3><ul>
<li>最终输出的是module.exports</li>
<li>而exports和module.exports是引用关系</li>
<li>exports指向module.exports</li>
</ul>
<blockquote>
<p>导入模块输出的数据<br>&lt;1&gt;. 添加属性时使用exports<br>&lt;2&gt; 而直接赋值时使用module.exports</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">exports.a = <span class="string">"ly"</span></span><br><span class="line">exports = <span class="function"><span class="keyword">function</span>(<span class="params"></span>)</span>&#123;&#125;</span><br><span class="line"><span class="comment">//直接赋值则改变了exports指向，此时得不到exports的值</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>模块化编程：不污染全局变量</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这是02.js</span></span><br><span class="line"><span class="keyword">let</span> n = <span class="string">"shellingfordly"</span></span><br><span class="line">exports.n = n</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//这是01.js文件</span></span><br><span class="line"><span class="comment">//require函数的返回值就是exports对象</span></span><br><span class="line"><span class="keyword">let</span> a = <span class="built_in">require</span>(<span class="string">"./02.js"</span>)</span><br><span class="line"><span class="built_in">console</span>.log(a)</span><br><span class="line"><span class="comment">//运行结果：&#123;n:"shellingfordly"&#125;</span></span><br></pre></td></tr></table></figure>

<h2 id="原生模块"><a href="#原生模块" class="headerlink" title="原生模块"></a>原生模块</h2><h3 id="模块引用"><a href="#模块引用" class="headerlink" title="模块引用"></a>模块引用</h3><ul>
<li>原生模块优先</li>
<li>不写路径时默认在node原生模块中找</li>
<li>默认找node_modules里的某个模块中的index.js运行</li>
</ul>
<h3 id="path模块"><a href="#path模块" class="headerlink" title="path模块"></a>path模块</h3><p>（1）路径变量</p>
<ul>
<li>__dirname 当前目录路径</li>
<li>__filename 当前文件路径</li>
</ul>
<p>（2）路径拼接</p>
<ul>
<li>path.join()<ul>
<li>单纯的拼接</li>
</ul>
</li>
<li>path.resolve()<ul>
<li>从当前目录出发</li>
<li>会解析”/“根目录</li>
</ul>
</li>
</ul>
<p>（3）path.relative</p>
<ul>
<li>得到两个文件之间的相对路径</li>
</ul>
<p>（4）path.parse</p>
<ul>
<li>格式化路径</li>
</ul>
<h3 id="url模块"><a href="#url模块" class="headerlink" title="url模块"></a>url模块</h3><ul>
<li>解析url</li>
</ul>
<blockquote>
<p>map数据类型：键 =&gt; 值<br>用get取值：map.get(键) = 值</p>
</blockquote>
<h3 id="events模块"><a href="#events模块" class="headerlink" title="events模块"></a>events模块</h3><p>(1) EventEmitter类</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"events"</span>).EventEmitter</span><br><span class="line"><span class="keyword">let</span> x = <span class="keyword">new</span> EventEmitter</span><br><span class="line"><span class="comment">//绑定事件</span></span><br><span class="line">x.on(<span class="string">"fn"</span>,()=&gt;&#123;&#125;)</span><br><span class="line"><span class="comment">//解绑事件</span></span><br><span class="line">x.off(<span class="string">"fn"</span>)</span><br><span class="line"><span class="comment">//触发事件</span></span><br><span class="line">x.emit(<span class="string">"fn"</span>)</span><br></pre></td></tr></table></figure>

<p>(2) newListener事件</p>
<ul>
<li>绑定后就会触发，不需要手动触发</li>
<li>新增监听器时触发</li>
</ul>
<p>(3) removeListener</p>
<ul>
<li>移除监听器时触发</li>
</ul>
<h3 id="require"><a href="#require" class="headerlink" title="require()"></a>require()</h3><p>导入模块</p>
<ul>
<li>可以自己定义想要导出的变量</li>
<li>每个模块的变量不对外公开</li>
</ul>
<h4 id="导入本地文件"><a href="#导入本地文件" class="headerlink" title="导入本地文件"></a>导入本地文件</h4><ul>
<li>必须加“./”</li>
<li>.js可以省略</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> obj = <span class="built_in">require</span>(<span class="string">"./2.js"</span>)</span><br><span class="line"><span class="comment">// .js可以省略，一样</span></span><br><span class="line"><span class="keyword">const</span> obj = <span class="built_in">require</span>(<span class="string">"./2"</span>)</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 运行index.js文件</span></span><br><span class="line">node index</span><br></pre></td></tr></table></figure>

<h3 id="nodejs执行顺序"><a href="#nodejs执行顺序" class="headerlink" title="nodejs执行顺序"></a>nodejs执行顺序</h3><blockquote>
<p>执行过程：执行script(全部代码)，清空micro-task队列；执行setInterval，清空micro-task队列；执行setTimeout，清空micro-task队列；执行setImmediate，清空micro-task队列；</p>
</blockquote>
<ul>
<li>有优先级<br>process.nextTick &gt; Promise().then &gt; setTimeout/setInterval &gt; setImmediate</li>
<li>macro-task<ul>
<li>script(全部代码)</li>
<li>setInterval</li>
<li>setTimeout</li>
<li>setImmediate</li>
</ul>
</li>
<li>setTimeout和setInterval优先级一样，谁先注册先执行谁</li>
<li>micro-task<ul>
<li>process.nextTick</li>
<li>Promise().then</li>
</ul>
</li>
</ul>
<h1 id="npm"><a href="#npm" class="headerlink" title="npm"></a>npm</h1><blockquote>
<p>node的包管理器</p>
</blockquote>
<h2 id="命令"><a href="#命令" class="headerlink" title="命令"></a>命令</h2><ul>
<li>初始化<ul>
<li>npm init 初始化</li>
<li>npm init -y 默认初始化，全部默认设置，不需要设置，不需要写项目名字等等</li>
</ul>
</li>
<li>删除包</li>
<li>注册<ul>
<li>npm login 登录</li>
<li>npm adduser 注册</li>
<li>npm publish 上传至npm官网</li>
<li>npm unpublish –force 删除</li>
</ul>
</li>
<li>安装<ul>
<li>npm install 安装</li>
<li>npm install -g 全局安装</li>
<li>npm i koa –save 上线需要用的</li>
<li>npm i koa@7.0.1 -S                 指定具体的版本，默认最高版本</li>
<li>npm i koa-router –save-dev</li>
</ul>
</li>
</ul>
<h2 id="koa-搭建web服务的框架"><a href="#koa-搭建web服务的框架" class="headerlink" title="koa 搭建web服务的框架"></a>koa 搭建web服务的框架</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> Koa = <span class="built_in">require</span>(<span class="string">"koa"</span>)</span><br><span class="line"><span class="keyword">const</span> app = <span class="keyword">new</span> Koa</span><br><span class="line"></span><br><span class="line">app.use(<span class="keyword">async</span>(ctx)=&gt;&#123;</span><br><span class="line">    ctx.body = <span class="string">"这是后台数据"</span></span><br><span class="line">&#125;)</span><br><span class="line">app.listen(<span class="number">3000</span>)</span><br></pre></td></tr></table></figure>

<ul>
<li>dependencies 项目上线所使用的依赖(某些模块)</li>
<li>devDependencies 开发环境所使用的</li>
</ul>
<h2 id="events"><a href="#events" class="headerlink" title="events"></a>events</h2><blockquote>
<p>事件模块</p>
</blockquote>
<h3 id="EventEmitter"><a href="#EventEmitter" class="headerlink" title="EventEmitter"></a>EventEmitter</h3><ul>
<li>emitter.on() 绑定事件</li>
<li>emitter.off() 注销事件</li>
<li>emitter.once() 绑定一次性事件</li>
<li>emitter.getMaxListeners() 返回最大监听值</li>
<li>emitter.listeners() 监听了多少个函数</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> EventEmitter = <span class="built_in">require</span>(<span class="string">"event"</span>).EventEmitter</span><br><span class="line"><span class="keyword">const</span> myEmitter = <span class="keyword">new</span> EventEmitter</span><br><span class="line">setTimeout( <span class="function"><span class="params">()</span>=&gt;</span>&#123;</span><br><span class="line">    <span class="comment">// 异步结果</span></span><br><span class="line">    <span class="comment">// 异步函数</span></span><br><span class="line">    <span class="comment">// 触发someEvents事件</span></span><br><span class="line">    myEmitter.emit(<span class="string">"someEvents"</span>)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 监听someEvents事件，返回一个回调函数，处理异步的结果，再异步结束后执行</span></span><br><span class="line">myEmitter.on(<span class="string">"someEvents"</span>, fn)</span><br></pre></td></tr></table></figure>

<h3 id="path"><a href="#path" class="headerlink" title="path"></a>path</h3><ul>
<li>__dirname 当前运行文件所在文件夹的路径</li>
<li>__filename 当前运行文件所在的路径，带文件名</li>
<li>path.join() 连接路径，相对路径</li>
<li>path.resolve() 返回绝对路径</li>
<li>path.parse() 解析路径，格式化</li>
</ul>
<h2 id="URL-解析URl"><a href="#URL-解析URl" class="headerlink" title="URL 解析URl"></a>URL 解析URl</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; URL &#125; = <span class="built_in">require</span>(<span class="string">"url"</span>)</span><br><span class="line"><span class="keyword">const</span> myUrl = <span class="keyword">new</span> URL(<span class="string">"http://......"</span>)</span><br></pre></td></tr></table></figure>

<h2 id="取查询字符串的键值对"><a href="#取查询字符串的键值对" class="headerlink" title="取查询字符串的键值对"></a>取查询字符串的键值对</h2><h3 id="querystring"><a href="#querystring" class="headerlink" title="querystring"></a>querystring</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// querystring 查询字符串</span></span><br><span class="line"><span class="keyword">const</span> qs = <span class="built_in">require</span>(<span class="string">"querystring"</span>)</span><br><span class="line"><span class="comment">// 不会去掉"?"</span></span><br><span class="line"><span class="comment">// 用slice切割掉"?"</span></span><br><span class="line"><span class="comment">// queryObj 查询信息对象</span></span><br><span class="line"><span class="keyword">const</span> queryObj = qs.parse(myUrl.search.slice(<span class="number">1</span>))</span><br><span class="line"><span class="built_in">console</span>.log(queryObj.wd)</span><br></pre></td></tr></table></figure>

<h3 id="断言"><a href="#断言" class="headerlink" title="断言"></a>断言</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> assert = <span class="built_in">require</span>(<span class="string">"assert"</span>)</span><br><span class="line">assert(<span class="string">"true"</span>,<span class="string">"如果第一个参数的布尔值不是true，则这个字符串就是报错信"</span>)</span><br></pre></td></tr></table></figure>

<h3 id="加密"><a href="#加密" class="headerlink" title="加密"></a>加密</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> crypto = <span class="built_in">require</span>(<span class="string">"crypto"</span>)</span><br><span class="line"><span class="keyword">const</span> KEY = <span class="string">"liuyao"</span></span><br><span class="line"><span class="keyword">const</span> obj = crypto.createHash(<span class="string">"md5"</span>)</span><br><span class="line">obj.update(KEY)</span><br><span class="line"><span class="keyword">const</span> password = obj.digest(<span class="string">"hex"</span>)</span><br><span class="line"><span class="built_in">console</span>.log(password)</span><br></pre></td></tr></table></figure>

<h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h2><blockquote>
<p>I/O操作</p>
</blockquote>
<h3 id="fs-readFile-读取文件"><a href="#fs-readFile-读取文件" class="headerlink" title="fs.readFile 读取文件"></a>fs.readFile 读取文件</h3><ul>
<li>fs.readFile (异步)读取文件</li>
<li>fs.readFileSync (同步)读取文件</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>)</span><br><span class="line"><span class="comment">// 将输出格式改为utf8</span></span><br><span class="line">fs.readFile(<span class="string">"./1.txt"</span>, <span class="string">"utf8"</span>, (err, data)=&gt;&#123;</span><br><span class="line">    <span class="comment">// err: json的错误对象</span></span><br><span class="line">    <span class="comment">// 不出错时err === null</span></span><br><span class="line">    <span class="comment">// data为2进制编码</span></span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="fs-writeFile-写文件"><a href="#fs-writeFile-写文件" class="headerlink" title="fs.writeFile 写文件"></a>fs.writeFile 写文件</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">"fs"</span>)</span><br><span class="line"><span class="keyword">const</span> data = <span class="string">"liuyao"</span></span><br><span class="line"></span><br><span class="line">fs.writeFile(<span class="string">"./2.txt"</span>, data, err =&gt; &#123;</span><br><span class="line">    <span class="keyword">if</span>(err) <span class="keyword">throw</span> err</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"写入成功"</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="fs-existsSync"><a href="#fs-existsSync" class="headerlink" title="fs.existsSync()"></a>fs.existsSync()</h3><blockquote>
<p>判断文件/或者路径是否存在，存在返回true，不存在返回false</p>
</blockquote>
<h3 id="fs-mkdir"><a href="#fs-mkdir" class="headerlink" title="fs.mkdir()"></a>fs.mkdir()</h3><blockquote>
<p>创建文件夹</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fs.mkdir(<span class="string">"./test"</span>, err =&gt; &#123;&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="fs-readdir"><a href="#fs-readdir" class="headerlink" title="fs.readdir()"></a>fs.readdir()</h3><blockquote>
<p>读文件夹</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fs.readdir(<span class="string">"./node"</span>, (err, data) =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(data)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="fs-statSync"><a href="#fs-statSync" class="headerlink" title="fs.statSync()"></a>fs.statSync()</h3><ul>
<li>stat.isDirectory 判断是否是文件夹</li>
<li>stat.isFile 判断是否是文件</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> stat = fs.statSync(<span class="string">"./node"</span>)</span><br><span class="line"><span class="built_in">console</span>.log( stat.isDirectory() )</span><br></pre></td></tr></table></figure>

<h2 id="流模块"><a href="#流模块" class="headerlink" title="流模块"></a>流模块</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 读取流</span></span><br><span class="line"><span class="keyword">const</span> read = fs.createReadStream(<span class="string">"1.txt"</span>)</span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">// 释放，无法操作文件数据</span></span><br><span class="line"><span class="comment">read.resume()</span></span><br><span class="line"><span class="comment">*/</span></span><br><span class="line"><span class="comment">// 设置编码格式</span></span><br><span class="line">read.setEncoding(<span class="string">"utf-8"</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 读取数据</span></span><br><span class="line">read.on(<span class="string">"data"</span>, data =&gt; &#123;</span><br><span class="line">    <span class="comment">// 读取到的数据</span></span><br><span class="line">    <span class="built_in">console</span>.log(data)</span><br><span class="line">&#125;)</span><br><span class="line"><span class="comment">// 读取结束后执行"死亡事件"</span></span><br><span class="line">read.on(<span class="string">"end"</span>, () =&gt; &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">"读取结束"</span>)</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h2 id="文件的复制"><a href="#文件的复制" class="headerlink" title="文件的复制"></a>文件的复制</h2><ul>
<li>read.pipe() 连接读取流和写入流的管道</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> read = fs.createReadStream(<span class="number">1.</span>txt)</span><br><span class="line"><span class="keyword">const</span> write = fs.createWriteStream(<span class="number">2.</span>txt)</span><br><span class="line"><span class="comment">// 将1.txt的文件复制到了2.txt中</span></span><br><span class="line">read.pipe(write)</span><br></pre></td></tr></table></figure>

<h1 id="http"><a href="#http" class="headerlink" title="http"></a>http</h1><ul>
<li>req.url 路径：根目录 /</li>
<li>req.method 请求方式GET</li>
<li>req.headers 请求头</li>
<li>text/plain 纯文本</li>
<li>text/html 解析标签</li>
</ul>
<h2 id="req-请求体对象"><a href="#req-请求体对象" class="headerlink" title="req 请求体对象"></a>req 请求体对象</h2><blockquote>
<p>从浏览器器输入的网址，把请求发送到对应后台时，所有的请求信息都被node封装在req对象里面</p>
</blockquote>
<ul>
<li>req.url 返回当前网页的路径（根目录 /）</li>
<li>req.method 请求方式GET</li>
<li>req.headers 请求头信息</li>
</ul>
<h2 id="res-对应的相应"><a href="#res-对应的相应" class="headerlink" title="res 对应的相应"></a>res 对应的相应</h2><blockquote>
<p>返回数据等</p>
</blockquote>
<ul>
<li>res.writeHead(200,{“Content-Type”:”text/plain; charset=utf-8”})<ul>
<li>请求头，设置编码格式</li>
<li>200 状态码</li>
<li>text/plain 数据的格式，纯文本</li>
<li>text/html 网页</li>
<li>utf-8 编码格式</li>
</ul>
</li>
<li>res.write() 想客户端返回数据<ul>
<li>write可以无数次写入</li>
</ul>
</li>
<li>res.end() 响应结束，客户端才能拿到数据<ul>
<li><strong>只能调用一次</strong></li>
<li>end传入的数据必须是字符串或者buffer二进制数据</li>
<li>end内的数据最后输出</li>
</ul>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> http = <span class="built_in">require</span>(<span class="string">"http"</span>)</span><br><span class="line"><span class="keyword">const</span> server = http.createServer( <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 请求头  </span></span><br><span class="line">    res.writeHead(<span class="number">200</span>,&#123;<span class="string">"Content-Type"</span>:<span class="string">"text/plain; charset=utf-8"</span>&#125;)</span><br><span class="line">    <span class="comment">// write写入数据</span></span><br><span class="line">    res.write(<span class="string">"后台返回的数据"</span>)</span><br><span class="line">    <span class="comment">// 拿到数据后结束</span></span><br><span class="line">    res.end(<span class="string">"。"</span>)</span><br><span class="line">&#125;)</span><br><span class="line">server.listen(<span class="number">3000</span>)</span><br></pre></td></tr></table></figure>

<h2 id="koa-views"><a href="#koa-views" class="headerlink" title="koa-views"></a>koa-views</h2><ul>
<li>把koa和视图框架链接起来</li>
</ul>
<h1 id="koa-router"><a href="#koa-router" class="headerlink" title="koa-router"></a>koa-router</h1><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> Koa = <span class="built_in">require</span>(<span class="string">'koa'</span>);</span><br><span class="line"><span class="keyword">var</span> Router = <span class="built_in">require</span>(<span class="string">'koa-router'</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> app = <span class="keyword">new</span> Koa();</span><br><span class="line"><span class="keyword">var</span> router = <span class="keyword">new</span> Router();</span><br><span class="line"></span><br><span class="line">router.get(<span class="string">'/'</span>, (ctx, next) =&gt; &#123;</span><br><span class="line">  <span class="comment">// ctx.router available</span></span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line">app</span><br><span class="line">  .use(router.routes())</span><br><span class="line">  .use(router.allowedMethods());</span><br></pre></td></tr></table></figure>

<h1 id="mongodb"><a href="#mongodb" class="headerlink" title="mongodb"></a>mongodb</h1><ul>
<li><p>mongod –dbpath ./db<br>启动服务端</p>
</li>
<li><p>mongod –dbpath ./db –port 3001<br>指定监听端口</p>
</li>
<li><p>mongod –dbpath ./db –logpath ./log<br>指定目录存放日志文件</p>
</li>
<li><p>mongo<br>连接</p>
</li>
<li><p>show dbs<br>查看当前数据库的集合</p>
</li>
<li><p>use 数据库名字<br>创建数据库</p>
</li>
</ul>
<h2 id="mongoose"><a href="#mongoose" class="headerlink" title="mongoose"></a>mongoose</h2><p>node管理mongodb的库</p>
<h3 id="mongoose-createConnection"><a href="#mongoose-createConnection" class="headerlink" title="mongoose.createConnection"></a>mongoose.createConnection</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 连接数据库</span></span><br><span class="line">mongoose.createConnection(<span class="string">"mongodb://localhost:27017/xxx"</span>, &#123;<span class="attr">useNewUrlParser</span>:<span class="literal">true</span>&#125;)</span><br></pre></td></tr></table></figure>
      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/weixin.jpg" alt="shellingford ly 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/zhifubao.jpg" alt="shellingford ly 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/node-js/" rel="tag"># node.js</a>
          
        </div>
      

      
      
        <div class="post-widgets">
        

        

        
          
          <div id="needsharebutton-postbottom">
            <span class="btn">
              <i class="fa fa-share-alt" aria-hidden="true"></i>
            </span>
          </div>
        
        </div>
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/29/hexo使用文档/" rel="prev" title="hexo使用文档">
                hexo使用文档 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/uploads/images/header.jpg" alt="shellingford ly">
            
              <p class="site-author-name" itemprop="name">shellingford ly</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives">
              
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">15</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">18</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/shellingfordly" target="_blank" title="github">
                      
                        <i class="fa fa-fw fa-github"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://gitee.com/shellingfordly" target="_blank" title="码云">
                      
                        <i class="fa fa-fw fa-maxcdn"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/shellingfordly" target="_blank" title="微博">
                      
                        <i class="fa fa-fw fa-weibo"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.cnblogs.com/shellingfordly" target="_blank" title="博客园">
                      
                        <i class="fa fa-fw fa-rss"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:shellingfordly@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i></a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://space.bilibili.com/288333302" target="_blank" title="bilibili">
                      
                        <i class="fa fa-fw fa-globe"></i></a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#了解node-js"><span class="nav-number">1.</span> <span class="nav-text">了解node.js</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#关于node"><span class="nav-number">1.1.</span> <span class="nav-text">关于node</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#安装nodejs"><span class="nav-number">1.2.</span> <span class="nav-text">安装nodejs</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#引用require"><span class="nav-number">1.3.</span> <span class="nav-text">引用require</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#输出exports"><span class="nav-number">1.4.</span> <span class="nav-text">输出exports</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#原生模块"><span class="nav-number">2.</span> <span class="nav-text">原生模块</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#模块引用"><span class="nav-number">2.1.</span> <span class="nav-text">模块引用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#path模块"><span class="nav-number">2.2.</span> <span class="nav-text">path模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#url模块"><span class="nav-number">2.3.</span> <span class="nav-text">url模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#events模块"><span class="nav-number">2.4.</span> <span class="nav-text">events模块</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#require"><span class="nav-number">2.5.</span> <span class="nav-text">require()</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#导入本地文件"><span class="nav-number">2.5.1.</span> <span class="nav-text">导入本地文件</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#nodejs执行顺序"><span class="nav-number">2.6.</span> <span class="nav-text">nodejs执行顺序</span></a></li></ol></li></ol><li class="nav-item nav-level-1"><a class="nav-link" href="#npm"><span class="nav-number"></span> <span class="nav-text">npm</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#命令"><span class="nav-number">1.</span> <span class="nav-text">命令</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#koa-搭建web服务的框架"><span class="nav-number">2.</span> <span class="nav-text">koa 搭建web服务的框架</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#events"><span class="nav-number">3.</span> <span class="nav-text">events</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#EventEmitter"><span class="nav-number">3.1.</span> <span class="nav-text">EventEmitter</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#path"><span class="nav-number">3.2.</span> <span class="nav-text">path</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#URL-解析URl"><span class="nav-number">4.</span> <span class="nav-text">URL 解析URl</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#取查询字符串的键值对"><span class="nav-number">5.</span> <span class="nav-text">取查询字符串的键值对</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#querystring"><span class="nav-number">5.1.</span> <span class="nav-text">querystring</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#断言"><span class="nav-number">5.2.</span> <span class="nav-text">断言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#加密"><span class="nav-number">5.3.</span> <span class="nav-text">加密</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件操作"><span class="nav-number">6.</span> <span class="nav-text">文件操作</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-readFile-读取文件"><span class="nav-number">6.1.</span> <span class="nav-text">fs.readFile 读取文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-writeFile-写文件"><span class="nav-number">6.2.</span> <span class="nav-text">fs.writeFile 写文件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-existsSync"><span class="nav-number">6.3.</span> <span class="nav-text">fs.existsSync()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-mkdir"><span class="nav-number">6.4.</span> <span class="nav-text">fs.mkdir()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-readdir"><span class="nav-number">6.5.</span> <span class="nav-text">fs.readdir()</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#fs-statSync"><span class="nav-number">6.6.</span> <span class="nav-text">fs.statSync()</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#流模块"><span class="nav-number">7.</span> <span class="nav-text">流模块</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#文件的复制"><span class="nav-number">8.</span> <span class="nav-text">文件的复制</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#http"><span class="nav-number"></span> <span class="nav-text">http</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#req-请求体对象"><span class="nav-number">1.</span> <span class="nav-text">req 请求体对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#res-对应的相应"><span class="nav-number">2.</span> <span class="nav-text">res 对应的相应</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#koa-views"><span class="nav-number">3.</span> <span class="nav-text">koa-views</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#koa-router"><span class="nav-number"></span> <span class="nav-text">koa-router</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#mongodb"><span class="nav-number"></span> <span class="nav-text">mongodb</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#mongoose"><span class="nav-number">1.</span> <span class="nav-text">mongoose</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#mongoose-createConnection"><span class="nav-number">1.1.</span> <span class="nav-text">mongoose.createConnection</span></a></li></ol></li></ol></li></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">shellingford ly</span>

  
</div>


  <div class="powered-by">
  <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a>
  </div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info"> 
  <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">
  NexT</a>
  </div>




<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">33.7k</span>
</div>

        
<div class="busuanzi-count">
  <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
      <div id="needsharebutton-float">
        <span class="btn">
          <i class="fa fa-share-alt" aria-hidden="true"></i>
        </span>
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'Cs5UqCDDJC5JezlK5wuzf2ql-gzGzoHsz',
        appKey: 'TC9fgcijHIjkjEwwCz6aQSJi',
        placeholder: '说点什么呢...',
        avatar:'monsterid',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  





  

  

  

  
  
  
  <link rel="stylesheet" href="/lib/needsharebutton/needsharebutton.css">

  
  
  <script src="/lib/needsharebutton/needsharebutton.js"></script>

  <script>
    
      pbOptions = {};
      
          pbOptions.iconStyle = "box";
      
          pbOptions.boxForm = "horizontal";
      
          pbOptions.position = "bottomCenter";
      
          pbOptions.networks = "Weibo,Wechat,QQZone,Facebook";
      
      new needShareButton('#needsharebutton-postbottom', pbOptions);
    
    
      flOptions = {};
      
          flOptions.iconStyle = "box";
      
          flOptions.boxForm = "horizontal";
      
          flOptions.position = "middleRight";
      
          flOptions.networks = "Weibo,Wechat,QQZone,Facebook";
      
      new needShareButton('#needsharebutton-float', flOptions);
    
  </script>

  

  

  

  

</body>
</html>
